﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using jQueryDotNetLib.Extenders;

namespace jQueryDotNetLib.Extras.Extenders
{
    public enum PositionOptions { left, right, top, bottom, notset }
    public enum jQZoomEffectOptions { show, fadein, hide, fadeout, notset }
    public enum jQZoomSpeedOptions { fast, slow, medium, notset }
    public enum jQZoomTypes { standard, reverse, notset }

    [ToolboxItem(true)]
    [ToolboxData("<{0}:jQZoom ThumbImgSrc=\"\" FullImgSrc=\"\"  runat=server />")]
    public class jQZoom : WebControl
    {
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public jQZoomTypes ZoomType
        {
            get
            {
                if (ViewState["ZoomType"] == null) return jQZoomTypes.notset;
                return (jQZoomTypes)ViewState["ZoomType"];
            }
            set
            {
                ViewState["ZoomType"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public int ZoomWidth
        {
            get
            {
                if (ViewState["ZoomWidth"] == null) return 0;
                return (int)ViewState["ZoomWidth"];
            }
            set
            {
                ViewState["ZoomWidth"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public int ZoomHeight
        {
            get
            {
                if (ViewState["ZoomHeight"] == null) return 0;
                return (int)ViewState["ZoomHeight"];
            }
            set
            {
                ViewState["ZoomHeight"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public PositionOptions Position
        {
            get
            {
                if (ViewState["Position"] == null) return PositionOptions.notset;
                return (PositionOptions)ViewState["Position"];
            }
            set
            {
                ViewState["Position"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public int XOffset
        {
            get
            {
                if (ViewState["XOffset"] == null) return 0;
                return (int)ViewState["XOffset"];
            }
            set
            {
                ViewState["XOffset"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public int YOffset
        {
            get
            {
                if (ViewState["YOffset"] == null) return 0;
                return (int)ViewState["YOffset"];
            }
            set
            {
                ViewState["YOffset"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public bool Lens
        {
            get
            {
                if (ViewState["Lens"] == null) return true;
                return (bool)ViewState["Lens"];
            }
            set
            {
                ViewState["Lens"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public float ImageOpacity
        {
            get
            {
                if (ViewState["ImageOpacity"] == null) return 0.2f;
                return (float)ViewState["ImageOpacity"];
            }
            set
            {
                ViewState["ImageOpacity"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public bool Title
        {
            get
            {
                if (ViewState["Title"] == null) return true;
                return (bool)ViewState["Title"];
            }
            set
            {
                ViewState["Title"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public bool ShowTitle
        {
            get
            {
                if (ViewState["ShowTitle"] == null) return true;
                return (bool)ViewState["ShowTitle"];
            }
            set
            {
                ViewState["ShowTitle"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public jQZoomEffectOptions ShowEffect
        {
            get
            {
                if (ViewState["ShowEffect"] == null) return jQZoomEffectOptions.notset;
                return (jQZoomEffectOptions)ViewState["ShowEffect"];
            }
            set
            {
                ViewState["ShowEffect"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public jQZoomEffectOptions HideEffect
        {
            get
            {
                if (ViewState["HideEffect"] == null) return jQZoomEffectOptions.notset;
                return (jQZoomEffectOptions)ViewState["HideEffect"];
            }
            set
            {
                ViewState["HideEffect"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public jQZoomSpeedOptions FadeInSpeed
        {
            get
            {
                if (ViewState["FadeInSpeed"] == null) return jQZoomSpeedOptions.notset;
                return (jQZoomSpeedOptions)ViewState["FadeInSpeed"];
            }
            set
            {
                ViewState["FadeInSpeed"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public jQZoomSpeedOptions FadeOutSpeed
        {
            get
            {
                if (ViewState["FadeOutSpeed"] == null) return jQZoomSpeedOptions.notset;
                return (jQZoomSpeedOptions)ViewState["FadeOutSpeed"];
            }
            set
            {
                ViewState["FadeOutSpeed"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public bool ShowPreload
        {
            get
            {
                if (ViewState["ShowPreload"] == null) return true;
                return (bool)ViewState["ShowPreload"];
            }
            set
            {
                ViewState["ShowPreload"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string PreloadText
        {
            get
            {
                if (ViewState["PreloadText"] == null) return "";
                return (string)ViewState["PreloadText"];
            }
            set
            {
                ViewState["PreloadText"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string PreloadPosition
        {
            get
            {
                if (ViewState["PreloadPosition"] == null) return "";
                return (string)ViewState["PreloadPosition"];
            }
            set
            {
                ViewState["PreloadPosition"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string ThumbImgSrc
        {
            get
            {
                if (ViewState["ThumbImgSrc"] == null) return "";
                return (string)ViewState["ThumbImgSrc"];
            }
            set
            {
                ViewState["ThumbImgSrc"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string FullImgSrc
        {
            get
            {
                if (ViewState["FullImgSrc"] == null) return "";
                return (string)ViewState["FullImgSrc"];
            }
            set
            {
                ViewState["FullImgSrc"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Text
        {
            get
            {
                if (ViewState["Text"] == null) return "";
                return (string)ViewState["Text"];
            }
            set
            {
                ViewState["Text"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Alt
        {
            get
            {
                if (ViewState["Alt"] == null) return "";
                return (string)ViewState["Alt"];
            }
            set
            {
                ViewState["Alt"] = value;
            }
        }

        protected void AddParameter(string Key, string Parameter)
        {
            if (Parameters == null) Parameters = new List<string>();
            if (Parameter != null && Parameter != "" && Key != null)
            {
                Parameters.Add(((Key != "") ? Key + ": " : "") + Parameter);
            }
        }
        protected void AddBinding(string Event, string EventHandle)
        {
            if (BindingEvents == null) BindingEvents = new List<string>();
            if (EventHandle != "") BindingEvents.Add("'" + Event + "', function(event, ui) {" + EventHandle + "}");
        }
        public List<string> Parameters { get; set; }
        public List<string> BindingEvents { get; set; }

        protected override void Render(HtmlTextWriter writer)
        {
            /*
             <a href="http://farm3.static.flickr.com/2104/2164650429_af2d3c7e9f.jpg?v=0" Title="Sneakers!" id="ZoomHref" runat="server">
<asp:Image ID="Image1"  Title="Hover here"   ImageUrl="http://farm3.static.flickr.com/2104/2164650429_af2d3c7e9f.jpg?v=0" runat="server" /> 
</a>

             */

            writer.Write(string.Format("<a id=\"{1}\" {2}  href=\"{0}\">", FullImgSrc, this.ClientID, (base.CssClass != null && base.CssClass != "") ? "Class=\"" + base.CssClass + "\"" : ""));
            writer.Write(string.Format("<img {2} {3} {1} {4} src=\"{0}\">", ThumbImgSrc, (Text != null && Text != "") ? "Title=\"" + Text + "\"" : "", (Height != Unit.Empty) ? "Height=\"" + Height + "\"" : "", (Width != Unit.Empty) ? "Width=\"" + Width + "\"" : "", (Alt != null && Alt != "") ? "Alt=\"" + Alt + "\"" : ""));
            writer.Write("</a>");

            if (ZoomType != jQZoomTypes.notset) AddParameter("zoomType", "'" + ZoomType.ToString() + "'");
            if (ZoomWidth != 0) AddParameter("zoomWidth", ZoomWidth.ToString());
            if (ZoomHeight != 0) AddParameter("zoomHeight", ZoomHeight.ToString());
            if (Position != PositionOptions.notset) AddParameter("position", "'" + Position.ToString() + "'");
            if (XOffset != 0) AddParameter("xOffset", XOffset.ToString());
            if (YOffset != 0) AddParameter("yOffset", YOffset.ToString());
            if (!Lens) AddParameter("lens", "false");
            if (!ShowTitle) AddParameter("title", "false");
            if (ShowEffect != jQZoomEffectOptions.notset) AddParameter("showEffect", "'" + ShowEffect.ToString() + "'");
            if (HideEffect != jQZoomEffectOptions.notset) AddParameter("hideEffect", "'" + HideEffect.ToString() + "'");
            if (FadeInSpeed != jQZoomSpeedOptions.notset) AddParameter("fadeinSpeed", "'" + FadeInSpeed.ToString() + "'");
            if (FadeOutSpeed != jQZoomSpeedOptions.notset) AddParameter("fadeoutSpeed", "'" + FadeOutSpeed.ToString() + "'");
            if (ImageOpacity != 0.2f) AddParameter("imageOpacity", ImageOpacity.ToString());
            if (!ShowPreload) AddParameter("showPreload", "false");
            if (PreloadText != "") AddParameter("preloadText", "'" + PreloadText + "'");
            if (PreloadPosition != "") AddParameter("preloadPosition", "'" + PreloadPosition + "'");
            //base.Render(writer);

            writer.Write("<script type=\"text/javascript\">");
            writer.Write("var ctl = jQuery(\"#" + this.ClientID + "\"); ctl.jqzoom({");

            if (Parameters != null && Parameters.Count > 0)
            {
                int count = Parameters.Count;
                int x = 0;
                foreach (string p in Parameters)
                {
                    writer.Write(p);
                    x++;
                    if (x < count) writer.Write(",");
                }
            }
            writer.Write("} );");
            if (BindingEvents != null && BindingEvents.Count > 0)
            {
                foreach (string evt in BindingEvents)
                {
                    writer.Write("ctl.bind(" + evt + ");");
                }
            }
            writer.Write("</script>");
        }

        protected override void OnInit(EventArgs e)
        {
            string webRes = Page.ClientScript.GetWebResourceUrl(this.GetType(), "jQueryDotNet.Extras.css.jqzoom.css");
            string cssLiteral = string.Format("<link type=\"text/css\" href=\"{0}\" rel=\"Stylesheet\" />", ResolveUrl(webRes));
            Page.Header.Controls.Add(new LiteralControl(cssLiteral));

            base.OnInit(e);
        }
    }
}
